import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.OutputStream;;

/**
 * file name:		TestZip.java
 * copyright:		© 2009 Kingsoft
 * author:			TanMenglong
 * version:			?.??
 * created:			2009-08-10
 * last modified:	2009-08-10
 * description:		HERE
 */

public class TestZip {
	public void zip(InputStream is, OutputStream os) {
		try {
			int BUFF_SIZE = 1024;
			ZipOutputStream zos = new ZipOutputStream(
					new BufferedOutputStream(os, BUFF_SIZE));

			zos.putNextEntry(new ZipEntry("data"));

			int size = 0;
			byte[] buffer = new byte[BUFF_SIZE];

			while ((size = is.read(buffer, 0, buffer.length)) != -1) {
				zos.write(buffer, 0, size);
			}

			zos.closeEntry();
			zos.flush();
			is.close();
			zos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String source = "/dustbin/test/test_in.txt";
		String dest = "/dustbin/test/test_out.zip";
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(source);
			fos = new FileOutputStream(dest);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos = new PipedOutputStream();
		try {
			pis.connect(pos);
		} catch (IOException e) {e.printStackTrace();}

		TestZip zipper = new TestZip();
		zipper.zip(fis, pos);
		byte[] buff = new byte[1024];
		try {
			int size = 0;
			while ((size = pis.read(buff)) != -1) {
//				System.out.println(buff.toString());
				fos.write(buff, 0, size);
				
			}
		} catch (IOException e) {e.printStackTrace();}
	}
}
